/**
 * @author ghp
 * @title
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return check(root, root);
    }

    /**
     * 判断二叉树是否对称
     *
     * @param left  左子树
     * @param right 右子树
     * @return
     */
    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            // 两者都为空，说明对称
            return true;
        }
        if (left == null || right == null) {
            // 一方为空，说明不对成
            return false;
        }
        // 单独判断根节点左右是否对称（除了第一遍，后面和f1是等价的）
        if (left.val != right.val) {
            return false;
        }
        // 判断外侧节点的值是否相同
        boolean f1 = check(left.left, right.right);
        // 判断右侧节点的值是否相同
        boolean f2 = check(left.right, right.left);
        return f1 && f2;
    }
}